﻿using Barcode_Project.Entity;
using Barcode_Project.Local;
using Barcode_Project.Network;
using DevExpress.XtraEditors;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Barcode_Project.RollerPrint
{
    public partial class PlateRollerPrint : DevExpress.XtraEditors.XtraForm
    {
        Dictionary<string, string> datas;
        HttpAgreement http;
        ILog _log;
        public PlateRollerPrint(Dictionary<string, string> pars)
        {
            _log = LogManager.GetLogger(GetType().Name);
            http = new HttpAgreement();
            datas = pars;

            InitializeComponent();

            new ComputerTask().LoadPrinter(select_printer);
            new ComputerTask().LoadTemplate(select_template);
        }

        private void lookUp_plateRollerCode_AutoSuggest(object sender, DevExpress.XtraEditors.Controls.LookUpEditAutoSuggestEventArgs e)
        {
            e.QuerySuggestions = new RemoteRequest().PlateRollerAutoSuggest(e.Text, e.CancellationToken);
        }

        private void lookUp_plateRollerCode_EditValueChanged(object sender, EventArgs e)
        {
            if (lookUp_plateRollerCode.EditValue != null)   //&& lookUp_plateRollerCode.EditValue.ToString().Length >= 8
            {
                LoadOldData();
            }
        }


        Dictionary<string, string> info { get; set; }
        void LoadOldData()
        {
            info = new RemoteRequest()
                .GetPlateRollerBranchInfo(lookUp_plateRollerCode.EditValue.ToString());

            CommonMethods.SetFormValue(group_data.Controls, info, "input_");

        }

        private void button_print_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!CheckBeforePrint())
            {
                return;
            }

            var json = CollectData();

            // 无需网络请求，已经拿到数据
            var dt = InfoObjToDataTable();

            var printerName = ((ItemSelector)select_printer.GetSelectedDataRow()).code;
            var templatePath = ((ItemSelector)select_template.GetSelectedDataRow()).code;

            var result = new ComputerTask().StartPrintingTask(dt, printerName, templatePath);

            if (result)
            {
                // 保存板架
                info["cuName"] = input_cuName.Text;
                info["memo"] = input_memo.Text;
                info["qcDate"] = input_qcDate.Text;
                info["warePosition"] = input_warePosition.Text;
                
                new RemoteRequest().SaveBanJia(info);

                var args = new XtraMessageBoxArgs();
                args.AutoCloseOptions.ShowTimerOnDefaultButton = true;
                args.DefaultButtonIndex = 0;
                args.AutoCloseOptions.Delay = 2000;
                args.Caption = "提示";
                args.Text = "打印任务发送成功";

                XtraMessageBox.Show(args);
            }
        }

        // 打印之前，检查必填项
        bool CheckBeforePrint()
        {
            var result = false;

            if (string.IsNullOrEmpty(input_byName.Text))
            {
                XtraMessageBox.Show("请选择版支");
                return result;
            }

            if (select_printer.GetSelectedDataRow() == null)
            {
                XtraMessageBox.Show("请选择打印机");
                return result;
            }
            if (select_template.GetSelectedDataRow() == null)
            {
                XtraMessageBox.Show("请选择打印模板");
                return result;
            }

            result = true;
            return result;
        }

        // 组织数据
        string CollectData()
        {
            return "";
        }

        DataTable InfoObjToDataTable()
        {
            var dt = new DataTable();

            PropertyDescriptorCollection props =
    TypeDescriptor.GetProperties(typeof(ViewPlateRollerInfo));

            foreach (PropertyDescriptor p in props)
            {
                if (p.Name != "id")
                {
                    dt.Columns.Add(p.Name, p.PropertyType);
                }
            }


            var row = dt.NewRow();

            row["byBarCode"] = info["byBarCode"];
            row["byCode"] = info["byCode"];
            row["byName"] = info["byName"];
            row["proCode"] = info["proCode"];
            row["proName"] = info["proName"];
            row["banchang"] = info["banchang"];
            row["banzhou"] = info["banzhou"];
            row["supCode"] = info["supCode"];
            row["wareHouse"] = info["wareHouse"];
            row["dTypes"] = info["dTypes"];
            row["createtime"] = info["createtime"];
            row["createuser"] = info["createuser"];
            row["money"] = info["money"];
            row["relationErpCode"] = info["relationErpCode"];
            row["PPlan"] = info["PPlan"];
            row["status"] = info["status"];
            row["isdel"] = info["isdel"];
            row["statusName"] = info["statusName"];
            row["houseName"] = info["houseName"];
            row["supName"] = info["supName"];


            row["cuName"] = input_cuName.Text;
            row["memo"] = input_memo.Text;
            row["qcDate"] = input_qcDate.Text;
            row["warePosition"] = input_warePosition.Text;

            dt.Rows.Add(row);

            return dt;
        }

        private void button_exit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Application.Exit();
        }

        private void btn_update_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var r = XtraMessageBox.Show("即将关闭程序进行升级，是否继续？", "提示", MessageBoxButtons.OKCancel);
            if (r == DialogResult.OK)
            {
                new RemoteRequest().UpdateCmdProgram();
            }
        }

        private void PlateRollerPrint_Load(object sender, EventArgs e)
        {
            input_qcDate.EditValue = DateTime.Now.ToString("yyyy-MM-dd");
        }
    }
}